/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ucm.model;

import cp3120_p.AirPoart;
import cp3120_p.DAO;
import cp3120_p.flights;
import javax.swing.JOptionPane;
import java.sql.*;
import java.util.Vector;
import sun.security.util.Password;

public class MainUI_Model {
    private int fromid = -1;
    private int arrid = -1;
    private DAO DbConn;
    public MainUI_Model() {
        DbConn = new DAO();
    }

    public int getArrid() {
        return arrid;
    }

    public void setArrid(int arrid) {
        this.arrid = arrid;
    }

    public int getFromid() {
        return fromid;
    }

    public void setFromid(int fromid) {
        this.fromid = fromid;
    }
    public String LoadDataFormDB()
    {
        String szInfo = "";
        Vector<AirPoart> allAirPoart = new Vector<AirPoart>();
        Vector<flights>  allflights = new Vector<flights>();
        Connection con = DbConn.getConnection();
        if (con == null) {
            szInfo = "can not connect to db";
            return szInfo;
        }
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            ResultSet res;
            res = stmt.executeQuery("select * from destinations");
            //res.first();
            while(res.next())
            {
                AirPoart poart = new AirPoart(res.getString("Destination"), res.getInt("AIRPOART_TAX"), res.getInt("GOOD_TAX"));
                allAirPoart.add(poart);
            }
            ResultSet res2;
            res2 = stmt.executeQuery("select * from flight");
//            res2.first();
            while(res2.next())
            {

                flights flight = new flights(res2.getString("FLIGHTID"), res2.getInt("FLIGHTDESTINATION"), res2.getInt("FLIGHTFROM") ,
                       res2.getInt("BelongID") , res2.getInt("FLIGHTSTATE") ,res2.getInt("PRICE"), res2.getInt("TA") );
                allflights.add(flight);
            }
            for (int i = 0; i < allflights.size(); i++)
            {
                String szFrom;
                String des;
                szFrom = allAirPoart.get(allflights.get(i).getFrom()).getName();
                des    = allAirPoart.get(allflights.get(i).getArr()).getName();
                szInfo += "Flight ID IS " + allflights.get(i).getId() + " from is " + szFrom + " destination is  " + des
                        + " price is : " + allflights.get(i).getPrice() + "\n";
            }
            return szInfo;

        } catch (Exception e) {
            System.out.println(e);
        }
        return szInfo;
    }
    
}
